package com.hh.bidding.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hh.bidding.domain.BidNotice;
import com.hh.bidding.domain.BidTenderRegistration;
import com.hh.bidding.service.IBidNoticeService;
import com.hh.bidding.service.IBidTenderRegistrationService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价Controller
 *
 * @author ruoyi
 * &#064;date  2025-10-16
 */
@RestController
@RequestMapping("/registration")
public class BidTenderRegistrationController extends BaseController
{
    @Resource
    private IBidTenderRegistrationService bidTenderRegistrationService;
    @Resource
    private IBidNoticeService bidNoticeService;

    /**
     * 查询招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BidTenderRegistration bidTenderRegistration)
    {
        startPage();
        List<BidTenderRegistration> list = bidTenderRegistrationService.selectBidTenderRegistrationList(bidTenderRegistration);
        return getDataTable(list);
    }

    /**
     * 导出招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价列表
     */
    @Log(title = "招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BidTenderRegistration bidTenderRegistration)
    {
        List<BidTenderRegistration> list = bidTenderRegistrationService.selectBidTenderRegistrationList(bidTenderRegistration);
        ExcelUtil<BidTenderRegistration> util = new ExcelUtil<BidTenderRegistration>(BidTenderRegistration.class);
        util.exportExcel(response, list, "招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价数据");
    }

    /**
     * 获取招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bidTenderRegistrationService.selectBidTenderRegistrationById(id));
    }

    @GetMapping(value = "/getInfoBySidAndHid/{hid}/{sid}")
    public AjaxResult getInfoBySidAndHid(@PathVariable("hid") Long hid,@PathVariable("sid") Long sid) {
        return success(bidTenderRegistrationService.selectBidTenderRegistrationByHidAndSid(hid,sid));
    }

    /**
     * 新增招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价
     */
    @Log(title = "招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BidTenderRegistration bidTenderRegistration)
    {
        boolean canRegistration = true;
        if(bidTenderRegistration.getUid() != null){
            BidNotice bidNotice = bidNoticeService.selectBidNoticeByUid(bidTenderRegistration.getUid());
            if(bidNotice != null && bidNotice.getRegistrationEndTime() != null){
                long time = bidNotice.getRegistrationEndTime().getTime();
                long now = System.currentTimeMillis();
                if(now > time){
                    canRegistration = false;//过了报名时间
                }
            }
        }
        if(canRegistration){
            return toAjax(bidTenderRegistrationService.insertBidTenderRegistration(bidTenderRegistration));
        }else{
            return AjaxResult.error();
        }
    }

    /**
     * 修改招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价
     */
    @Log(title = "招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BidTenderRegistration bidTenderRegistration)
    {
        return toAjax(bidTenderRegistrationService.updateBidTenderRegistration(bidTenderRegistration));
    }

    /**
     * 删除招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价
     */
    @Log(title = "招标项目竞价报名，供应商报名后才能参与投标，最后才能参与竞价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bidTenderRegistrationService.deleteBidTenderRegistrationByIds(ids));
    }
}
